#
# The Counter::Email class
# Handling special operations on records from the Email file
#
package Counter::Email;

use POSIX qw(strftime);
use Carp;

use strict;
use vars qw(@ISA %lmap);
use FileTable::TSRecord;

@ISA = qw(FileTable::TSRecord);

%lmap = (
"SYNTAX" => 1,
"DNS" => 2,
"SMTP" => 3,
"MAIL" => 4,
"ADMIN" => 5  # Patched to bad by a human
	 );

sub setstate {
    my $self = shift;
    my ($state, $level, $error, $force) = @_;
    
    # Reject state change from BAD to TEMPFAIL, unless new level is higher
    if ($self->{state} eq "bad" && $state eq "tempfail" && !$force
	&& $lmap{$self->{level}} >= $lmap{$level}) {
	carp $self->key(), ": No change from BAD at $$self{level} to $state at $level allowed";
	return 0;
    }
    # Reject change from BAD at a high level to OK at a lower level
    if ($self->{state} eq "bad" && $state ne "bad"
	&& $lmap{$self->{level}} > $lmap{$level} && !$force) {
	carp $self->key(), ": No change from BAD at $$self{level} to $state at $level allowed";
	return 0;
    }
    # Debug...
    croak "Illegal level: $level" if !$lmap{$level};
    $self->{state} = $state;
    $self->{error} = $error;
    $self->{level} = $level;
    $self->{checktool} = $0;
    $self->{checkdate} = strftime "%Y-%m-%d %H:%M:%S", gmtime();
    return 1;
}

1;
